#include <stdio.h>
#include <SDL/SDL.h>
int main(int argc, char *argv[]) {
    int done = 1;
	SDL_Surface *screen;
	SDL_Surface *image;
    Uint8 *keystate;
	SDL_Surface *temp;
	SDL_Rect src, dest;
	SDL_Event event;
	if (SDL_Init(SDL_INIT_VIDEO) != 0) {
		printf("Unable to initialize SDL: %s\n", SDL_GetError());
		return 1;
	}

	screen = SDL_SetVideoMode(798, 601, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
	if (screen == NULL) {
		printf("Unable to set video mode: %s\n", SDL_GetError());
		return 1;
	}
	temp= SDL_LoadBMP("img/right.bmp");
	SDL_SetColorKey(temp, SDL_SRCCOLORKEY, SDL_MapRGB(temp->format, 255, 255, 255));
	if (temp == NULL) {
		printf("Unable to load bitmap: %s\n", SDL_GetError());
		return 1;
	}

	image = SDL_LoadBMP("img/image.bmp");
	if (image == NULL) {
		printf("Unable to load bitmap: %s\n", SDL_GetError());
		return 1;
	}

	src.x = 0;
	src.y = 0;
	src.w = image->w;
	src.h = image->h;

	dest.x = 0;
	dest.y = 0;
	dest.w = image->w;
dest.h = image->h;
SDL_BlitSurface(image, &src, screen, &dest);
	while(done)
{

SDL_BlitSurface(image, 0, screen, 0);
	SDL_BlitSurface(temp, &src, screen, &dest);
SDL_UpdateRect(screen, 0, 0, 0, 0);
	while (SDL_PollEvent(&event))
{

	switch (event.type)
	{

		case SDL_QUIT:
		done = 0;
		break;

		case SDL_KEYDOWN:
		{

			if (event.key.keysym.sym == SDLK_ESCAPE)
			done = 0;
			break;
		}
	}
		keystate = SDL_GetKeyState(NULL);
	 if (keystate[SDLK_LEFT] ) {
    temp = SDL_LoadBMP("img/left.bmp");
   	SDL_SetColorKey(temp, SDL_SRCCOLORKEY, SDL_MapRGB(temp->format, 255, 255, 255));
      dest.x -= 5;
    }
    if (keystate[SDLK_RIGHT] ) {
    temp = SDL_LoadBMP("img/right.bmp");
    SDL_SetColorKey(temp, SDL_SRCCOLORKEY, SDL_MapRGB(temp->format, 255, 255, 255));
      dest.x += 5   ;
    }
    if (keystate[SDLK_UP] ) {
    temp = SDL_LoadBMP("img/up.bmp");
    SDL_SetColorKey(temp, SDL_SRCCOLORKEY, SDL_MapRGB(temp->format, 255, 255, 255));
      dest.y -= 5;
    }
	if (keystate[SDLK_DOWN] ) {
    temp = SDL_LoadBMP("img/down.bmp");
    SDL_SetColorKey(temp, SDL_SRCCOLORKEY, SDL_MapRGB(temp->format, 255, 255, 255));
      dest.y += 5;
    }
     if ( dest.x > 698 ) {
      dest.x = 698;
    }
    if ( dest.y > 243 ) {
      dest.x = 243;
    }
SDL_Flip(screen);
}
}

	SDL_FreeSurface(image);
	SDL_FreeSurface(temp);

	return 0;
}
